package com.smz.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.smz.entity.TMenu;
import com.smz.entity.TMoveWarehouse;
import com.smz.service.TMenuService;
import com.smz.service.TMoveWarehouseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;

@Api
@RestController
public class TMoveWarehouseController {
    @Autowired
    private TMoveWarehouseService tMoveWarehouseService;
    @Autowired
    private TMenuService tMenuService;

    @ApiOperation("找到所有移入库表")
    @GetMapping("findTMoveWarehouse")
    public ResponseEntity<Page<List<TMoveWarehouse>>> TMoveWarehouse(@RequestParam(value = "current", defaultValue = "1") Integer current, @RequestParam("row") Integer row) {

        Page page = new Page<>(current, row);
        Page<List<TMoveWarehouse>> page1 = tMoveWarehouseService.page(page);
        page1.setTotal(tMoveWarehouseService.count());
        return ResponseEntity.ok(page1);
    }
    @ApiOperation("添加入库")
    @PostMapping("/addTmove")
    public ResponseEntity<String> TMoveWarehouse(@RequestBody TMoveWarehouse tMoveWarehouse) {
      //设id
        tMoveWarehouse.setId(0);
        Random r = new Random();
        //编号随机数
        String s = String.valueOf(r.nextInt(1000));
        tMoveWarehouse.setMvNo(s);
        //入库名称
        tMoveWarehouse.getMvIn();
        TMenu one = tMenuService.getOne(new QueryWrapper<TMenu>().lambda().eq(TMenu::getWareNo, tMoveWarehouse.getMvIn()));
        tMoveWarehouse.setMvIn(one.getWareName());
        //移入库名称
        tMoveWarehouse.getMvOut();
        TMenu out = tMenuService.getOne(new QueryWrapper<TMenu>().lambda().eq(TMenu::getWareNo, tMoveWarehouse.getMvOut()));
        tMoveWarehouse.setMvOut(out.getWareName());
        //添加
        boolean save = tMoveWarehouseService.save(tMoveWarehouse);
        if(save){
            return ResponseEntity.ok("ok");
        }else {
            return  ResponseEntity.ok("exit");
        }


    }
}
